// src/logger/index.js
const winston = require("winston");

// 创建 Winston 日志记录器
const logger = winston.createLogger({
  level: "info",
  format: winston.format.combine(
    winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
    winston.format.metadata({ fillExcept: ["message", "level", "timestamp"] }),
    winston.format.printf(({ level, message, timestamp, metadata }) => {
      const label =
        metadata && metadata.label ? metadata.label : "default-label";
      console.log("Log metadata:", { level, message, timestamp, label }); // 调试日志
      return `${timestamp} [${label}] ${level}: ${message}`;
    })
  ),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: "error.log", level: "error" }),
    new winston.transports.File({ filename: "combined.log" }),
  ],
});

// 如果不是生产环境，将日志输出到控制台
if (process.env.NODE_ENV !== "production") {
  logger.add(
    new winston.transports.Console({
      format: winston.format.combine(
        winston.format.colorize(),
        winston.format.simple()
      ),
    })
  );
}

// 自定义日志记录方法
const customLogger = {
  info: (tag, message) => {
    console.log("Logging with tag:", tag); // 调试日志
    logger.info(message, { label: tag });
  },
  error: (tag, message) => {
    console.log("Logging with tag:", tag); // 调试日志
    logger.error(message, { label: tag });
  },
  warn: (tag, message) => {
    console.log("Logging with tag:", tag); // 调试日志
    logger.warn(message, { label: tag });
  },
  debug: (tag, message) => {
    console.log("Logging with tag:", tag); // 调试日志
    logger.debug(message, { label: tag });
  },
};

module.exports = customLogger;
